//
//  PriceView.m
//  MeiXiu
//
//  Created by Yuan on 13-1-20.
//
//

#import <QuartzCore/QuartzCore.h>
#import "PriceView.h"
#import "UIColor+ColorUtils.h"

@implementation PriceView

- (id)initWithFrame:(CGRect)frame withArray:(NSArray *)arr selected:(NSInteger)selected
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
//        self.backgroundColor = [UIColor colorWithRed:0.2 green:0.6 blue:0.2 alpha:0.55];
        
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
        v.backgroundColor = [UIColor clearColor];
        v.layer.cornerRadius = 6.0f;
        v.userInteractionEnabled = YES;
        v.layer.masksToBounds = YES;
        [self addSubview:v];
                
        UIImage *lineimg = [UIImage imageNamed:@"hulu_xian.png"];
        UIButton *xianbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [xianbtn setBackgroundImage:[lineimg stretchableImageWithLeftCapWidth:lineimg.size.width/2 topCapHeight:0] forState:UIControlStateNormal];
        CGRect lineFrame = CGRectMake(25, 20, frame.size.width, lineimg.size.height);
        [xianbtn setFrame:lineFrame];
        xianbtn.userInteractionEnabled = NO;
        [v addSubview:xianbtn];
        
        UIButton *lBtn = [self cBtnImg:[UIImage imageNamed:@"hulu_zuo.png"] point:CGPointMake(20, 20)];
        [lBtn setImageEdgeInsets:UIEdgeInsetsMake(-24, -5, 0, 0)];
        lBtn.tag = 1;
        [v addSubview:lBtn];
    
        UILabel *lLbl = [self cLbl:CGRectMake(5, 60, 60, 25) title:[arr objectAtIndex:0]];
        [v addSubview:lLbl];
        lLbl.tag = 5;
        
        if (selected==0) {
            lBtn.selected = YES;
            [lLbl setTextColor:[UIColor colorWithString:@"#f376a0"]];
        }
        
        [lLbl release];
        
        
        
        UIButton *mBtn1 = [self cBtnImg:[UIImage imageNamed:@"hulu_zhong"] point:CGPointMake(85, 20)];
        [mBtn1 setImageEdgeInsets:UIEdgeInsetsMake(-24,0, 0, 0)];
        mBtn1.tag = 2;
        [v addSubview:mBtn1];
        
        UILabel *mLbl1 = [self cLbl:CGRectMake(mBtn1.frame.origin.x - 16, 60, 60, 25) title:[arr objectAtIndex:1]];
        [v addSubview:mLbl1];
        mLbl1.tag = 6;
        if (selected==1) {
            mBtn1.selected = YES;
            [mLbl1 setTextColor:[UIColor colorWithString:@"#f376a0"]];
        }
        [mLbl1 release];
        
        UIButton *mBtn2 = [self cBtnImg:[UIImage imageNamed:@"hulu_zhong"] point:CGPointMake(150, 20)];
        [mBtn2 setImageEdgeInsets:UIEdgeInsetsMake(-24,0, 0, 0)];
        mBtn2.tag = 3;
        [v addSubview:mBtn2];
        
        UILabel *mLbl2 = [self cLbl:CGRectMake(mBtn2.frame.origin.x - 16, 60, 60, 25) title:[arr objectAtIndex:2]];
        [v addSubview:mLbl2];
        mLbl2.tag = 7;
        if (selected==2) {
            mBtn2.selected = YES;
            [mLbl2 setTextColor:[UIColor colorWithString:@"#f376a0"]];
        }
        [mLbl2 release];
        
        UIButton *rBtn = [self cBtnImg:[UIImage imageNamed:@"hulu_you"] point:CGPointMake(215, 20)];
        [rBtn setImageEdgeInsets:UIEdgeInsetsMake(-24,7, 0, 0)];
        rBtn.tag = 4;
        [v addSubview:rBtn];
        
        UILabel *rLbl = [self cLbl:CGRectMake(rBtn.frame.origin.x - 16, 60, 60, 25) title:[arr objectAtIndex:3]];
        [v addSubview:rLbl];
        rLbl.tag = 8;
        if (selected==3) {
            rBtn.selected = YES;
            [rLbl setTextColor:[UIColor colorWithString:@"#f376a0"]];
        }
        [rLbl release];
        
        v.tag = 1;
        
        [v release];
        
        lineFrame.size.width = rBtn.frame.size.width + rBtn.frame.origin.x - lineFrame.origin.x;
        xianbtn.frame = lineFrame;
    }
    return self;
}

-(UILabel *)cLbl:(CGRect)rect title:(NSString *)t
{
    UILabel *showLbl = [[UILabel alloc]initWithFrame:rect];
    showLbl.font = [UIFont systemFontOfSize:12.0f];
    [showLbl setTextColor:[UIColor grayColor]];
    [showLbl setBackgroundColor:[UIColor clearColor]];
    [showLbl setText:t];
    [showLbl setTextAlignment:UITextAlignmentCenter];
    return showLbl;
}

-(UIButton *)cBtnImg:(UIImage *)lImg point:(CGPoint)point
{
    UIButton *lBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [lBtn setFrame:CGRectMake(point.x,point.y, lImg.size.width, lImg.size.height)];
    [lBtn setContentEdgeInsets:UIEdgeInsetsMake(10, 0, 0, 0)];
    [lBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [lBtn setTitleColor:[UIColor colorWithString:@"#f376a0"] forState:UIControlStateSelected];
    [lBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [lBtn.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
    
    UIImage *selectImg = [UIImage imageNamed:@"huluxingshi_d"];
    [lBtn setImage:selectImg forState:UIControlStateHighlighted];
    [lBtn setImage:selectImg forState:UIControlStateSelected];
    [lBtn setImage:nil forState:UIControlStateNormal];
    [lBtn setBackgroundImage:lImg forState:UIControlStateHighlighted];
    [lBtn setBackgroundImage:lImg forState:UIControlStateNormal];
    [lBtn setBackgroundImage:lImg forState:UIControlStateSelected];
    return lBtn;
}

-(void)clickBtn:(UIButton *)btn
{
    UIView *v =  [self viewWithTag:1];
    
    for(UIView *sv in v.subviews)
    {
        if ([sv isKindOfClass:[UIButton class]]) {
            [(UIButton *)sv setSelected:NO];
        }
        if ([sv isKindOfClass:[UILabel class]]) {
            [(UILabel *)sv setTextColor:[UIColor grayColor]];
        }
    }
    btn.selected = YES;
    UILabel *lbl = (UILabel *)[self viewWithTag:4+btn.tag];
    [lbl setTextColor:[UIColor colorWithString:@"#f376a0"]];
    pBlock(btn.tag,lbl.text);
}

-(void)setPriceViewBlock:(PriceBlock)bl
{
    pBlock = Block_copy(bl);
}


-(void)hideme:(UIButton *)g
{
    [self removeFromSuperview];
}

- (void)dealloc
{
    Block_release(pBlock);
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
